<?php
// +----------------------------------------------------------------------
// | 云静Admin
// +----------------------------------------------------------------------
// | Copyright (c) 2019-2020 http://www.yunj.net.cn
// +----------------------------------------------------------------------
// | 云静Admin提供个人非商业用途免费使用。
// +----------------------------------------------------------------------
// | Author: Uncle-L <1732983738@qq.com>
// +----------------------------------------------------------------------
// | 注意：
// | 1、配置 'url_common_param'=>true
// +----------------------------------------------------------------------
// | 初始化（禁止更改代码顺序）
// +----------------------------------------------------------------------

namespace app\yunj\behavior;

class Init{

    public function appInit($params){
        $this->yunjInit();
        $this->cors();
    }

    // 云静初始化
    private function yunjInit(){
        // 检测PHP环境
        if(version_compare(PHP_VERSION,'7.3','<'))  die('PHP版本过低，最少需要PHP7.3，请升级PHP版本！');

        if(!defined('YUNJ_PATH')) define('YUNJ_PATH',env('app_path').'yunj/');
        if(!defined('YUNJ_VERSION')) define('YUNJ_VERSION',include YUNJ_PATH.'library/version.php');

        // 定义yunj模块的命名空间
        \think\Loader::addNamespace('yunj',YUNJ_PATH.'library');

        // 引入公共方法库
        include YUNJ_PATH.'common.php';
        // 引入路由文件
        include YUNJ_PATH.'route.php';
    }

    // CORS跨域请求处理
    private function cors(){
        $origin=yunj_config('cors.access_control_allow_origin','*');
        $origin=$origin?:'*';
        $methods=yunj_config('cors.access_control_allow_methods','');
        $methods=$methods&&(substr($methods,0,1)!==',')?','.$methods:$methods;
        $headers=yunj_config('cors.access_control_allow_headers','');
        $headers=$headers&&(substr($headers,0,1)!==',')?','.$headers:$headers;
        header('Access-Control-Allow-Origin:'.$origin);
        header('Access-Control-Allow-Methods:POST,GET,PUT,DELETE,OPTIONS'.$methods);
        header("Access-Control-Allow-Headers:token,Origin,X-Requested-With,Content-Type,Accept".$headers);
        if (request()->isOptions()) exit();
    }

}